<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/static/common/default.jsp"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	</head>
	
	<body>
		<div class="breadcrumbs" id="breadcrumbs">
	
			<ul class="breadcrumb">
				<li><i class="icon-home home-icon"></i> <a href="#">首页</a></li>
				<li>用户管理</li>
				<li class="active">编辑</li>
			</ul>
			
		</div>
		
		<div class="page-content">
			<div class="page-header">
				<h1>教师管理
					<small>
					<i class="icon-double-angle-right"></i>
					编辑
					</small>					
				</h1>		
			</div> 
			<div class="row">
				<div class="col-xs-12">
					<form class="form-horizontal" onsubmit="return sysUserInfoAdd();" role="form" id="sysUserForm" name="sysUserForm" method="post">
						
						<div class="form-group" >
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">登录名</label>
							<div class="col-sm-9">
								<input type="text" id="loginName"  name="loginName" value="${res.loginName}" readonly = "readonly" placeholder="登录名" class="col-xs-10 col-sm-5" size="50" />
							</div>
						</div>
						<div class="form-group"  style="display: none" >
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">密码</label>
							<div class="col-sm-9">
								<input type="password" id="passWord"  name="passWord" value="" placeholder="密码" class="col-xs-10 col-sm-5" size="50" />
							</div>
						</div>
						<div class="form-group" style="display: none">
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">密码确认</label>
							<div class="col-sm-9">
								<input type="password" id="respassWord"  name="respassWord" value="" placeholder="密码确认" class="col-xs-10 col-sm-5" size="50" />
							</div>
						</div>
						<div class="form-group" >
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">姓名</label>
							<div class="col-sm-9">
								<input type="text" id="userName"  name="userName" value="${res.userName}" placeholder="姓名" class="col-xs-10 col-sm-5" size="50" />
							</div>
						</div>
						<div class="form-group" >
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">邮箱</label>
							<div class="col-sm-9">
								<input type="text" id="email"  name="email" value="${res.email}" placeholder="邮箱" class="col-xs-10 col-sm-5" size="50" />
							</div>
						</div>
						<div class="form-group" >
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">电话</label>
							<div class="col-sm-9">
								<input type="text" id="phoneNum"  name="phoneNum" value="${res.phoneNum}" placeholder="电话" class="col-xs-10 col-sm-5" size="50" />
							</div>
						</div>
						<div class="form-group" >
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">性别</label>
							<div class="col-sm-9">
								<c:choose>
									<c:when test="${res.sex==1}">
										<input name="sex" type="radio" checked="checked" value="1" />男 
										<input name="sex" type="radio" value="0" />女 
									</c:when>
									<c:otherwise>
										<input name="sex" type="radio" value="1" />男 
										<input name="sex" type="radio" checked="checked" value="0" />女 
									</c:otherwise>
								</c:choose>
							</div>
						</div>
						<div class="form-group" >
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">角色</label>
							<div class="col-sm-9">
								<select  id="roleId" name ="roleId">
									<option value="2">教师</option>
								</select> 
							</div>
						</div>
						<div class="form-group" >
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">科目</label>
							<div class="col-sm-9">
							<select id="subjectId">
							</select>
							</div>
						</div>
						
						<div class="form-group" >
							<label class="col-sm-3 control-label no-padding-right" for="form-field-1">是否禁用</label>
							<div class="col-sm-9">
								<select  id="isAvailable" name ="isAvailable">
									<option value="0" <c:if test="${res.isAvailable==0}">selected</c:if>>是</option>
									<option value="1" <c:if test="${res.isAvailable==1}">selected</c:if>>否</option>
								</select> 
							</div>
							
						</div>
						
						<input type="hidden" id="user_id" name="user_id" value="${res.user_id}">
						<input type="hidden" id="status" name="status" value="${res.status}">
					
						<div class="clearfix form-actions">
							<div class="col-md-offset-3 col-md-9">
								<button class="btn btn-info" type="submit">
									<i class="icon-ok bigger-110"></i>提交
								</button>
							</div>
						</div>
						
					</form>		
				</div>
							
			</div>
		</div>
		
		<script type="text/javascript">
		$(function(){
			var subjectId = "${res.subjectId}";
			 $.ajax({
			        async: true,
			        type:"POST",
			        data:null,
			        url: "${ctx}/sys/tsubject/listall",
			        dataType: "json",
			        success: function (data) {
			    	    if("success" == data.result){//成功
							if(data.list.length>0){
								for(var a=0;a<data.list.length;a++){
									if(subjectId == data.list[a].id){
										$("#subjectId").prepend("<option selected = 'selected' value='"+data.list[a].id+"'>"+data.list[a].name+"</option>");
									}else{
										$("#subjectId").prepend("<option value='"+data.list[a].id+"'>"+data.list[a].name+"</option>");
									}
									
								}
							}else{
								
							}
			    	    }else{//失败
			    	    	alert("学校加载失败");
			    	    }
			        }
				},"json");
		 });
			function sysUserInfoAdd() {
				if (hassamepassword()) {
					var datastr = new Object();
					datastr.user_id = $('#user_id').val();
	    			datastr.loginName = $('#loginName').val();
	    			datastr.password = $('#passWord').val();
	    			datastr.userName = $('#userName').val();
	    			datastr.email = $('#email').val();
	    			datastr.phoneNum = $('#phoneNum').val();
	    			datastr.status = $('#status').val();
	    			datastr.isAvailable = $('#isAvailable').val();
	    			datastr.sex = $("input[name='sex']:checked").val();
 	    			datastr.roleId = $('#roleId').val();
 	    			datastr.subjectId = $('#subjectId').val();
					var result = true;
					$.ajax({
    		        async: true,
    		        type:"POST",
    		        data:JSON.stringify(datastr),
    		        url: "${ctx}/sys/user/updateUser",
    		        dataType: "json",
    		        headers : {  
                        'Content-Type' : 'application/json;charset=utf-8'  
                    },
	                    success : function(data) {
	                		if (data != undefined && data.status != undefined
	                				&& data.status.trim() == '0') {
	                			result = true;
	                			alert("保存成功！");
	                			window.location.href = "${ctx}/sys/user/listUser";
	                		} else {
	                			result = false;
	                			alert(data.error);
	                		}
	                	},
	    			},"json");
				}
				return false;
				
			}
			
			function hassamepassword(){
				var password = $('#passWord').val();
				var respassword = $('#respassWord').val();
				if(password == respassword){
					return true;
				}else{
					return false;
				}
			}
			
		</script>
	</body>


</html>